C
C     ..................................................................
C
C        SUBROUTINE DRTWI
C
C        PURPOSE
C           TO SOLVE GENERAL NONLINEAR EQUATIONS OF THE FORM X=FCT(X)
C           BY MEANS OF WEGSTEIN-S ITERATION METHOD.
C
C        USAGE
C           CALL DRTWI (X,VAL,FCT,XST,EPS,IEND,IER)
C           PARAMETER FCT REQUIRES AN EXTERNAL STATEMENT.
C
C        DESCRIPTION OF PARAMETERS
C           X      - DOUBLE PRECISION RESULTANT ROOT OF EQUATION
C                    X=FCT(X).
C           VAL    - DOUBLE PRECISION RESULTANT VALUE OF X-FCT(X)
C                    AT ROOT X.
C           FCT    - NAME OF THE EXTERNAL DOUBLE PRECISION FUNCTION
C                    SUBPROGRAM USED.
C           XST    - DOUBLE PRECISION INPUT VALUE WHICH SPECIFIES THE
C                    INITIAL GUESS OF THE ROOT X.
C           EPS    - SINGLE PRECISION INPUT VALUE WHICH SPECIFIES THE
C                    UPPER BOUND OF THE ERROR OF RESULT X.
C           IEND   - MAXIMUM NUMBER OF ITERATION STEPS SPECIFIED.
C           IER    - RESULTANT ERROR PARAMETER CODED AS FOLLOWS
C                     IER=0 - NO ERROR,
C                     IER=1 - NO CONVERGENCE AFTER IEND ITERATION STEPS,
C                     IER=2 - AT ANY ITERATION STEP THE DENOMINATOR OF
C                             ITERATION FORMULA WAS EQUAL TO ZERO.
C
C        REMARKS
C           THE PROCEDURE IS BYPASSED AND GIVES THE ERROR MESSAGE IER=2
C           IF AT ANY ITERATION STEP THE DENOMINATOR OF ITERATION
C           FORMULA WAS EQUAL TO ZERO. THAT MEANS THAT THERE IS AT
C           LEAST ONE POINT IN THE RANGE IN WHICH ITERATION MOVES WITH
C           DERIVATIVE OF FCT(X) EQUAL TO 1.
C
C        SUBROUTINES AND FUNCTION SUBPROGRAMS REQUIRED
C           THE EXTERNAL DOUBLE PRECISION FUNCTION SUBPROGRAM FCT(X)
C           MUST BE FURNISHED BY THE USER.
C
C        METHOD
C           SOLUTION OF EQUATION X=FCT(X) IS DONE BY MEANS OF
C           WEGSTEIN-S ITERATION METHOD, WHICH STARTS AT THE INITIAL
C           GUESS XST OF A ROOT X. ONE ITERATION STEP REQUIRES ONE
C           EVALUATION OF FCT(X). FOR TEST ON SATISFACTORY ACCURACY SEE
C           FORMULAE (2) OF MATHEMATICAL DESCRIPTION.
C           FOR REFERENCE, SEE
C           (1) G. N. LANCE, NUMERICAL METHODS FOR HIGH SPEED COMPUTERS,
C               ILIFFE, LONDON, 1960, PP.134-138,
C           (2) J. WEGSTEIN, ALGORITHM 2, CACM, VOL.3, ISS.2 (1960),
C               PP.74,
C           (3) H.C. THACHER, ALGORITHM 15, CACM, VOL.3, ISS.8 (1960),
C               PP.475,
C           (4) J.G. HERRIOT, ALGORITHM 26, CACM, VOL.3, ISS.11 (1960),
C               PP.603.
C
C     ..................................................................
C
      SUBROUTINE DRTWI(X,VAL,FCT,XST,EPS,IEND,IER)
C
C
      DOUBLE PRECISION X,VAL,FCT,XST,A,B,D,TOL
C
C     PREPARE ITERATION
      IER=0
      TOL=XST
      X=FCT(TOL)
      A=X-XST
      B=-A
      TOL=X
      VAL=X-FCT(TOL)
C
C
C     START ITERATION LOOP
      DO 6 I=1,IEND
      IF(VAL)1,7,1
C
C     EQUATION IS NOT SATISFIED BY X
    1 B=B/VAL-1.D0
      IF(B)2,8,2
C
C     ITERATION IS POSSIBLE
    2 A=A/B
      X=X+A
      B=VAL
      TOL=X
      VAL=X-FCT(TOL)
C
C     TEST ON SATISFACTORY ACCURACY
      TOL=EPS
      D=DABS(X)
      IF(D-1.D0)4,4,3
    3 TOL=TOL*D
    4 IF(DABS(A)-TOL)5,5,6
    5 IF(DABS(VAL)-1.D1*TOL)7,7,6
    6 CONTINUE
C     END OF ITERATION LOOP
C
C
C     NO CONVERGENCE AFTER IEND ITERATION STEPS. ERROR RETURN.
      IER=1
    7 RETURN
C
C     ERROR RETURN IN CASE OF ZERO DIVISOR
    8 IER=2
      RETURN
      END
